﻿/// <reference path="../jqGrid/jqGrid-4.6.0/jquery.jqGrid.src.js" />
/// <reference path="../jqGrid/jqGrid-4.6.0/i18n/grid.locale-en.js" />
/// <reference path="../jQuery/jquery-1.11.0.js" />
/// <reference path="../jQuery-UI/jquery-ui-1.10.4.custom.js" />
/// <reference path="../jQuery-UI/i18n/jquery.ui.datepicker-en-GB.js" />
/// <reference path="../qTip2/qTip2-2.2.0/jquery.qtip.js" />
/// <reference path="../vpGrid/vpGrid-1.2.4.js" />
/// <reference path="../vpGrid/i18n/vpGrid-1.2.2.locale-en.js" />



(function (window, $, vpGrid, undefined) {
    var MyCompany = window.MyCompany || {};
    MyCompany.MyGrid = MyCompany.MyGrid || {};
    var myGrid = MyCompany.MyGrid;
    try {
        
        onSummaryCellRenderingEventhandler = function (source, eventArgs) {
            /// <signature>
            /// <summary>This event handler will enable you to define the content of any summary cell, 
            ///          its format,its CSS and any additional addtional HTML attributes on the cell container.</summary>
            /// <param name="source" type="Object">the vpGrid object itself</param>
            /// <param name="eventArgs" type="Object">Object that is initialized by vpGrid and used by vpGrid once this event handler has finished its processing.
            /// the eventArgs parameter has the following signature : 
            ///     eventArgs = {
            ///       cellName: cellname,
            ///       cellValue: value,
            ///       useCellFormatter : false,
            ///       uiIdentifier : xxxx  -> value of the data tag data-vpgrid-ui-id
            ///       td : {},
            ///       remove :{}
            ///       isAsync: true -> this event is async. you cannot revert it back to be synchronous
            ///       asyncInit : function(){} -> call this method at the beginning of your event handler
            ///       asyncEnd : function(){}  -> call this method when your event handler has finished its task (be it asynchronous or synchronous)
            ///     };
            ///
            /// The event handler can set the eventArgs.td literal object with properties corresponding to html attributes.
            /// The uiIdentifier property enables listener to know on which summary row he is called:
            ///         if there is only one summary row (this is the default): uiIdentifier=""
            ///         if there are more than one summary row: uiIdentifier = "SumarryRowN" where N = 1,2,3, ...
            /// If existing attributes or classes must be removed, the event handler should fill the remove property.
            /// For example, to remove the ui-state-error class on the td : remove.class='ui-state-error'.
            /// for example : eventArgs.td.title = 'xxx', eventArgs.td.class = 'myclass', eventArgs.td.style="xxx'
            /// The Event handler should also setup the cellValue property. This value will be rendered in the cell summary.
            /// If cellValue must be rendered in the same format as other cells in the column, the event handler should explicitely set the useCellFormatter property to true.
            /// </param>
            /// </signature>
            try {
                var colName = eventArgs.cellName;
                if (colName === "price") {
                    eventArgs.asyncInit();
                    var result = vpGrid.sumOf(colName);
                    eventArgs.cellValue = result;
                    eventArgs.useCellFormatter = true;
                    eventArgs.asyncEnd();
                    return;
                }

            } catch (e) {
                vpGrid.logException(e);
            }
        }

        onDataReceivedEventhandler = function (source, eventArgs) {
            /// <signature>
            /// <summary> This event is raised when the response data (for a READ operation) has been received from remote server.
            /// this event enables to change the received data before loading it in the grid.
            /// for example the server might sent "compressed data" or "unreadable" data to optimize security and/or bandwidth
            /// this event enables listener to transform a non readable received data into a meaningfull data.
            /// Listeners might also add or remove any data to or from the received data.
            /// </summary>
            /// <param name="source" type="Object">the vpGrid object itself</param>
            /// <param name="eventArgs" type="Object">Object that is initialized by vpGrid and used by vpGrid once this event handler has finished its processing.
            /// the eventArgs parameter has the following signature : 
            ///     eventArgs = {
            ///       data: [{col1:xxx, col2:yyy, ... colN:zzz}, ...]
            ///     };
            /// The data property of the eventArgs object is an array of objects. Each object in the array represent a row in the grid. 
            /// The property names of these objects must be the name of an existing column in the grid.
            /// </param>
            /// </signature>
            try {
                var data = eventArgs.data;
                var itemCount = data.length;
                for (var i = 0; i < itemCount; i++) {
                    var item = data[i];
                    if (item.style === "U") {
                        item.style = "Universal";
                        continue;
                    }
                    if (item.style === "W") {
                        item.style = "Women's";
                        continue;
                    }

                    if (item.style === "M") {
                        item.style = "Men's";
                        continue;
                    }


                }
                
            } catch (e) {
                vpGrid.logException(e);
            }
        }

        onUpdatedDataReceivedEventhandler = function (source, eventArgs) {
            /// <signature>
            /// <summary> This event is raised when the response data has been received from remote server due to a data update on server-side.
            /// this event enables to do any post processing on updated data
            /// for example the caller might upload files or do any ajax request on the real updated data
            /// </summary>
            /// <param name="source" type="Object">the vpGrid object itself</param>
            /// <param name="eventArgs" type="Object">Object that is initialized by vpGrid and used by vpGrid once this event handler has finished its processing.
            /// the eventArgs parameter has the following signature : 
            ///     eventArgs = {
            ///              sentData: {col1:xxx, col2:yyy, ... colN:zzz, extraData1:ttt, extraData2:uuu, ..., extraDataN:www},     -> Data sent to the server to make the server-side update
            ///              receivedData: {col1:xxx, col2:yyy, ... colN:zzz}, -> Data sent back by the server after the server-side update operation
            ///              keyName:      -> name of the key that identifies the entity on server side
            ///              keyValue:       -> value of the key
            ///      };
            /// The sentData property of the eventArgs object represents the client-side updated row in the grid.
            /// receivedData is the server-side updated entity : 
            ///         - it should contain the entity key found in sentData
            ///         - and any property that has been changed on server side (for example the grid can show a server-side calculated column)
            ///         - every property found in receivedData that are different from the sentData will be updated in the grid after this event handler has executed    
            /// this event enables to change the received data before loading it in the grid.
            /// for example the server might sent "compressed data" or "unreadable" data to optimize security and/or bandwidth
            /// this event enables listener to transform a non readable received data into a meaningfull data.
            /// The property names of the sentdata and receivedData must be the name of an existing column in the grid.
            /// </param>
            /// </signature>
            try {
                var data = eventArgs.receivedData;
                var itemCount = data.length;
                for (var i = 0; i < itemCount; i++) {
                    var item = data[i];
                    if (item.style === "U") {
                        item.style = "Universal";
                        continue;
                    }
                    if (item.style === "W") {
                        item.style = "Women's";
                        continue;
                    }

                    if (item.style === "M") {
                        item.style = "Men's";
                        continue;
                    }


                }

            } catch (e) {
                vpGrid.logException(e);
            }
        }

        onInsertedDataReceivedEventhandler = function (source, eventArgs) {
            try {
                var data = eventArgs.receivedData;
                var itemCount = data.length;
                for (var i = 0; i < itemCount; i++) {
                    var item = data[i];
                    if (item.style === "U") {
                        item.style = "Universal";
                        continue;
                    }
                    if (item.style === "W") {
                        item.style = "Women's";
                        continue;
                    }

                    if (item.style === "M") {
                        item.style = "Men's";
                        continue;
                    }


                }

            } catch (e) {
                vpGrid.logException(e);
            }
        }

        onCellValidatingEventhandler = function (source, eventArgs) {
            try {
                
            } catch (e) {
                vpGrid.logException(e);
            }
        }

        onRowEditingEventhandler = function (source, eventArgs) {
            try {
                
            } catch (e) {
                vpGrid.logException(e);
            }
        };

        onRowInsertedEventhandler = function (source, eventArgs) {
            try {

            } catch (e) {
                vpGrid.logException(e);
            }
        };

        onCellRenderingEventhandler = function (source, eventArgs) {
            try {
                var data = eventArgs.rowData;
                var cellName = eventArgs.cellName;
                var cellValue = eventArgs.cellValue;

            } catch (e) {
                vpGrid.logException(e);
            }
        };

        onSelectCellRenderingEventhandler = function (source, eventArgs) {
            try {

            } catch (e) {
                vpGrid.logException(e);
            }
        };

        onAutocompleteCellRenderingEventhandler = function (source, eventArgs) {
            try {

            } catch (e) {
                vpGrid.logException(e);
            }
        };

        onImageCellRenderingEventhandler = function (source, eventArgs) {
            try {

            } catch (e) {
                vpGrid.logException(e);
            }
        };

        onLinkCellRenderingEventhandler = function (source, eventArgs) {
            try {

            } catch (e) {
                vpGrid.logException(e);
            }
        };

        onCellEditingEventhandler = function (source, eventArgs) {
            try {
                
            } catch (e) {
                vpGrid.logException(e);
            }
        };

        onInlineSummaryRowRenderingEventhandler = function (source, eventArgs) {
            try {

            } catch (e) {
                vpGrid.logException(e);
            }
        };

        onCellUpdatingEventhandler = function (source, eventArgs) {
            try {

            } catch (e) {
                vpGrid.logException(e);
            }
        };

        onCellUpdatedEventhandler = function (source, eventArgs) {
            /// <signature>
            /// <summary>This event handler is called when a cell has been updated on client-side (after its content has been edited)
            ///          This event handler is raised before the complete row is sent to server for an insert/update operation.         
            ///</summary>
            /// <param name="source" type="Object">the vpGrid object itself</param>
            /// <param name="eventArgs" type="Object">Object that is initialized by vpGrid and used by vpGrid once this event handler has finished its processing.
            /// the eventArgs parameter has the following signature : 
            /// the eventArgs parameter has the following signature : 
            ///     eventArgs = {
            ///       cellName, -> name of the cell that has been edited 
            ///       cellValue, -> value of the edited cell
            ///       rowid, -> id of the HTML table row that holds the updated cell
            ///       rowData -> literal object that contains all cells in the row : {col1:xxx, col2:yyy, ... colN:zzz}
            ///     };
            /// The event handler uses eventArgs properties to make any additional processing on the grid. 
            /// For example: 
            ///     - changing other cells of the same row by applying specific business rules.
            ///         -> to change another cell, the event listener may set the rowData.colName property of the corresponding cell
            ///     - updating the summary rows by calling vpGrid.renderSummaryRows()
            /// </param>
            /// </signature>
            try {
                var colName = eventArgs.cellName;
                if (colName === "price") {
                    // update any other column upon specific business rules
                }

            } catch (e) {
                vpGrid.logException(e);
            }
        }

        onModalDialogOpeningEventhandler = function (source, eventArgs) {
            try {
                var dialogId = eventArgs.dialogId;
                var rowData = eventArgs.rowData;

                if (dialogId === "serverSideDialog1") {
                    var itemId = rowData.remoteId;
                    var url = eventArgs.iframeUrl;
                    url = url + "?ItemId=" + itemId + "&IsInFrame=true";
                    eventArgs.iframeUrl = url;
                    return;
                }

            } catch (e) {
                vpGrid.logException(e);
            }
        };

        onModalDialogClosingEventhandler = function (source, eventArgs) {
            try {
                var dialogId = eventArgs.dialogId;
                var rowData = eventArgs.rowData;

            } catch (e) {
                vpGrid.logException(e);
            }
        };

        onModalDialogClosedEventhandler = function (source, eventArgs) {
            try {
                var dialogId = eventArgs.dialogId;
                var rowData = eventArgs.rowData;

            } catch (e) {
                vpGrid.logException(e);
            }
        };

        onModalDialogOpenedEventhandler = function (source, eventArgs) {
            try {
                var dialogId = eventArgs.dialogId;
                var rowData = eventArgs.rowData;

            } catch (e) {
                vpGrid.logException(e);
            }
        };

        onFormValidatingEventhandler = function (source, eventArgs) {
            try {

            } catch (e) {
                vpGrid.logException(e);
            }
        };

        onFileuploadingEventhandler = function (source, eventArgs) {
            try {

            } catch (e) {
                vpGrid.logException(e);
            }
        };

        onUIElementDisablingEventhandler = function (source, eventArgs) {
            try {

            } catch (e) {
                vpGrid.logException(e);
            }
        };
        onUIElementEnablingEventhandler = function (source, eventArgs) {
            try {

            } catch (e) {
                vpGrid.logException(e);
            }
        };

        onGridLoadedEventhandler = function (source, eventArgs) {
            try {

            } catch (e) {
                vpGrid.logException(e);
            }
        };

        onUserInfoReadyEventhandler = function (source, eventArgs) {
            try {

            } catch (e) {
                vpGrid.logException(e);
            }
        };

        onGlobalInfoReadyEventhandler = function (source, eventArgs) {
            try {

            } catch (e) {
                vpGrid.logException(e);
            }
        };

        onWindowClosingEventhandler = function (source, eventArgs) {
            /// <signature>
            /// <summary>This event handler will enable you to to cancel the closing of the browser window upon specific business rules 
            /// </summary>
            /// <param name="source" type="Object">the vpGrid object itself</param>
            /// <param name="eventArgs" type="Object">Object that is initialized by vpGrid and used by vpGrid once this event handler has finished its processing.
            /// the eventArgs parameter has the following signature : 
            ///      eventArgs = {
            ///        data: {}, -> literal object that holds the data that needs to be sent to the server (see vpGrid.getData())
            ///        cancel : false|true (false by default)
            ///        cancelReason : ""
            ///      };
            /// The event handler can inspect the data object, but MUST NOT modify it
            /// To prevent the window from closing, the event handler must set the cancel property to true. 
            /// Optionnaly the event handler should set a message to tell the user why the window cannot be closed.
            /// </param>
            /// </signature>
            try {
                var data = eventArgs.data;
                // Check your business rules against the grid data found in eventArgs
               
                //eventArgs.cancel = true;
                //eventArgs.cancelReason = "Custom message";

            } catch (e) {
                vpGrid.logException(e);
            }
        };

        onDataRowValidatingEventhandler = function (source, eventArgs) {
            /// <signature>
            /// <summary>This event handler will enable you to validate upon specific business rules a row that has been inserted in the grid 
            ///             (a new row created on client-side with the method vpGrid.insertNewRow() ) 
            /// </summary>
            /// <param name="source" type="Object">the vpGrid object itself</param>
            /// <param name="eventArgs" type="Object">Object that is initialized by vpGrid and used by vpGrid once this event handler has finished its processing.
            /// the eventArgs parameter has the following signature : 
            ///     eventArgs = {
            ///       rowData: data, -> literal object that holds the data of a row 
            ///       isValid: true|false
            ///       errors : [] -> array of objects containing all errors in the dataRow
            ///                     each object in the array must have the following signature:
            ///                     {
            ///                         propertyName : string -> name of a column/field
            ///                         message : string -> put here the error message for the specified column/field
            ///                     }
            ///     };
            /// The event handler can inspect the data object, but MUST NOT modify it
            /// The event handler should set the eventArgs.isValid property to false if custom validation fails.
            /// If validation fails, the Event handler should push all error messages as object literals in the errors array :
            ///       for example if the column price is not valid :
            ///             eventArgs.errors.push({ propertyName: "price", message: "The price cannot be so high!" });
            /// </param>
            /// </signature>
            try {
                var data = eventArgs.rowData;
                // Check your business rules against the grid data found in eventArgs

                //eventArgs.isValid = false;
                //eventArgs.errors.push({ propertyName: "price", message: "Price is incorrect" });

            } catch (e) {
                vpGrid.logException(e);
            }
        };

        //All vpGrid events are registered only for demonstration purpose
        //In production do not register to events you don't care
        vpGrid.registerEventHandler(vpGrid.Event.onDataReceived, onDataReceivedEventhandler);
        vpGrid.registerEventHandler(vpGrid.Event.onUpdatedDataReceived, onUpdatedDataReceivedEventhandler);
        vpGrid.registerEventHandler(vpGrid.Event.onInsertedDataReceived, onInsertedDataReceivedEventhandler);
        vpGrid.registerEventHandler(vpGrid.Event.onCellRendering, onCellRenderingEventhandler);
        vpGrid.registerEventHandler(vpGrid.Event.onSelectCellRendering, onSelectCellRenderingEventhandler);
        vpGrid.registerEventHandler(vpGrid.Event.onAutocompleteCellRendering, onAutocompleteCellRenderingEventhandler);
        vpGrid.registerEventHandler(vpGrid.Event.onImageCellRendering, onImageCellRenderingEventhandler);
        vpGrid.registerEventHandler(vpGrid.Event.onLinkCellRendering, onLinkCellRenderingEventhandler);
        vpGrid.registerEventHandler(vpGrid.Event.onCellUpdating, onCellUpdatingEventhandler);
        vpGrid.registerEventHandler(vpGrid.Event.onCellUpdated, onCellUpdatedEventhandler);
        vpGrid.registerEventHandler(vpGrid.Event.onSummaryCellRendering, onSummaryCellRenderingEventhandler);
        vpGrid.registerEventHandler(vpGrid.Event.onInlineSummaryRowRendering, onInlineSummaryRowRenderingEventhandler);
        vpGrid.registerEventHandler(vpGrid.Event.onRowEditing, onRowEditingEventhandler);
        vpGrid.registerEventHandler(vpGrid.Event.onRowInserted, onRowInsertedEventhandler);
        vpGrid.registerEventHandler(vpGrid.Event.onCellEditing, onCellEditingEventhandler);
        vpGrid.registerEventHandler(vpGrid.Event.onCellValidating, onCellValidatingEventhandler);
        vpGrid.registerEventHandler(vpGrid.Event.onModalDialogOpening, onModalDialogOpeningEventhandler);
        vpGrid.registerEventHandler(vpGrid.Event.onModalDialogOpened, onModalDialogOpenedEventhandler);
        vpGrid.registerEventHandler(vpGrid.Event.onModalDialogClosing, onModalDialogClosingEventhandler);
        vpGrid.registerEventHandler(vpGrid.Event.onModalDialogClosed, onModalDialogClosedEventhandler);
        vpGrid.registerEventHandler(vpGrid.Event.onFormValidating, onFormValidatingEventhandler);
        vpGrid.registerEventHandler(vpGrid.Event.onFileuploading, onFileuploadingEventhandler);
        vpGrid.registerEventHandler(vpGrid.Event.onUIElementDisabling, onUIElementDisablingEventhandler);
        vpGrid.registerEventHandler(vpGrid.Event.onUIElementEnabling, onUIElementEnablingEventhandler);
        vpGrid.registerEventHandler(vpGrid.Event.onGridLoaded, onGridLoadedEventhandler);
        vpGrid.registerEventHandler(vpGrid.Event.onUserInfoReady, onUserInfoReadyEventhandler);
        vpGrid.registerEventHandler(vpGrid.Event.onGlobalInfoReady, onGlobalInfoReadyEventhandler);
        vpGrid.registerEventHandler(vpGrid.Event.onWindowClosing, onWindowClosingEventhandler);
        vpGrid.registerEventHandler(vpGrid.Event.onDataRowValidating, onDataRowValidatingEventhandler);

        // Dom event Handlers
        myGrid.onSelectChangedEventHandler1 = function (source, eventArgs) {
            try {
                var selectedId = $(source.target).val();

                if (eventArgs === "kindOfProducts") {
                    
                    var url = "./StartTemplate.html?TypeOfProduct=" + selectedId
                    window.location.href = url;
                }

            } catch (e) {
                vpGrid.logException(e);
            }
        };


    } catch (e) {
        vpGrid.logException(e);
    }
    finally {
        window.MyCompany = MyCompany;
    }


})(this, jQuery, vpGrid);
